<?php

namespace app\models;

use Yii;
use app\models\App;

/**
 * This is the model class for table "images".
 *
 * @property string $id
 * @property string $app_id
 * @property string $group_id
 * @property string $image
 * @property integer $content_length
 * @property string $source_type
 * @property string $create_time
 */
class Images extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public $path;
    public $cont;
    public $cont_type;
    public static function tableName()
    {
        return 'images';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['path', 'cont', 'cont_type'], 'required']
        ];
    }
    /**
     * 移动图片到分组
     * @author 黄宇翔
     * @access public
     * @param mixed $groupId 分组id
     * @param mixed $imagesId 图片id
     * @since 1.0
     * @return array
     */
    public function updateImages($groupId, $imagesId)
    {
        return images::find()->createCommand()->update('images', array(
            'group_id' => $groupId
        ), "id='$imagesId'")->execute();
    }

    /**
     * 获取图片分组id
     * @author 黄宇翔
     * @access public
     * @param mixed $imagesId 图片id
     * @since 1.0
     * @return array
     */
    public function findGroupId ($imagesId, $appId)
    {
        $sql = "select group_id from images where id='$imagesId' AND app_id='$appId'";
        return Yii::$app->db->createCommand($sql)->execute();//之前有分组

    }
    /**
     * 插入图片数据
     * @author 黄宇翔
     * @access public
     * @param mixed $data 图片数组
     * @since 1.0
     * @return array
     */
    public function insertImages($data)
    {
        return  images::find()->createCommand()->insert('images', $data)->execute();
    }
      /**
     * 删除图片数据
     * @author 黄宇翔
     * @access public
     * @param mixed $name 名字
     * @since 1.0
     * @return bool
     */
    public function deleteImages ($name, $appId)
    {
        $model = images::find()->where(['id' => $name, 'app_id' => $appId])->one();
        if ($model) {
            $model->delete();
            return true;
        }
        return false;
    }
    /**
     *按照分页读取信息

     * @author 黄宇翔
     * @author $appId 应用id
     * @author $limit 分页数
     * @author $pageno 分页数
     * @access public
     * @since 1.0
     * @return boolean
     */
    public function findByItems ($appId, $limit, $pageno, $groupId)
    {
        $imageDate = array(
            'app_id' => $appId,
        );
        if (!empty($groupId)) {
            if($groupId == 'ungrouped'){
                $imageDate['group_id'] = null;
            }
            else {
                $imageDate['group_id'] = $groupId;
            }
        }

        //验证appid是否存在
        $storeCount = $this::find()->where($imageDate)->count('id');
        $pageno = ($pageno - 1) * $limit;
        $data['items'] = $this::find()->where($imageDate)->limit($limit)->offset($pageno)->orderBy('create_time desc')->all();
        $data['pageno'] = ceil($storeCount/$limit);
        $data['total'] = $storeCount;
        return $data;
    }
}
